package com.wydexcel.generate.process;

import com.wydexcel.generate.properties.s.ExcelFormatProperties;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;

import java.util.HashMap;
import java.util.Map;

public class WydExcelCellFormatProcessImpl implements WydExcelCellFormatProcess {
    private Map<String, DataFormat> map = new HashMap<>();
    private Map<String, Short> dataMap = new HashMap<>();

    @Override
    public Short formatProcess(Row row, Cell cell, ExcelFormatProperties property, boolean isBody) {
        try {

            if (null == property) {
                return null;
            }
            if (isBody && property.getIsHeader() == 1) {
                return null;
            }
            if (!isBody && property.getIsHeader() == 2) {
                return null;
            }
            String key = property.getFieldName() + property.getIsHeader();
            DataFormat dataFormat = map.get(key);
            if (null == dataFormat) {
                dataFormat = row.getSheet().getWorkbook().createDataFormat();
                map.put(key, dataFormat);
            }

            Short aShort = dataMap.get(key);
            if (null == aShort) {
                aShort = dataFormat.getFormat(property.getDataformat());
                dataMap.put(key, aShort);
            }
            return aShort;

        } catch (Exception e) {
            return null;
        }

    }

}
